package com.autumn.xa.a.mq.demo;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.springframework.stereotype.Component;

/**
 * ClassName: Producer
 *
 * @Description:
 * @Author: wangqiuyang
 * @Date: 2021/11/4 5:11 下午
 * History:
 * <author>          <time>          <version>          <desc>
 * wangqiuyang    2021/11/4 5:11 下午   @Version 1.0        描述
 */
@Component
public class Producer {
    private String producerGroup = "producer_group";

    private DefaultMQProducer producer;

    public Producer(){
        producer = new DefaultMQProducer(producerGroup);
        //指定nameserver地址，多个地址使用,分隔
        producer.setNamesrvAddr(RocketMQConfig.NAME_SERVER);
        start();
    }

    public DefaultMQProducer getProducer(){
        return this.producer;
    }

    public void start(){
        try {
            this.producer.start();
        } catch (MQClientException e) {
            e.printStackTrace();
        }
    }

    public void shutdown(){
        this.producer.shutdown();
    }
}
